#include<stdlib.h>
#include<iostream>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

#define SIZE 512
#define NUM 30

char* gargv[NUM];
char pwd[NUM*2];


void split(char* command,char** gargv)
{
    gargv[0]=strtok(command," ");
    int i = 1;
    while(gargv[i++]=strtok(NULL," "));
}

int main(int argc, char *argv[], char *env[])
{
    while(true)
    {
        string user = getenv("USER");
        string path = getenv("PWD");
        cout << user + '@' + "MV-20-16-ubuntu:" + path + '>' + ' ';
        fflush(stdout);
        char command[SIZE];
        fgets(command, sizeof(command), stdin);
        command[strlen(command)-1]='\0';
        // cout<<n<<endl;
        split(command, gargv);

        if(strcmp("cd",gargv[0])==0)
        {
            char* path = gargv[1];
            chdir(path);
            char tmp[NUM*2];
            getcwd(tmp,sizeof tmp);
            snprintf(pwd,sizeof tmp+4,"PWD=%s",tmp);
            putenv(pwd);
            continue;
        }

        pid_t pid = fork();
        if (pid == 0)
        {
            execvp(gargv[0],gargv);
            exit(0);
        }
        else if(pid>0)
        {
            waitpid(pid, NULL, 0);
        }
    }
    return 0;
}